import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// 主游戏类
public class AdvancedPlaneWarGame extends JFrame {
    private PlayerPlane playerPlane;
    private List<EnemyPlane> enemyPlanes;
    private List<Bullet> bullets;
    private Timer timer;
    private Random random;
    private Image dbImage;
    private Graphics dbg;
    private int score;
    private boolean gameOver;

    public AdvancedPlaneWarGame() {
        initGame();

        setTitle("高级飞机大战");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (gameOver) {
                    initGame();
                } else {
                    playerPlane.keyPressed(e, bullets);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!gameOver) {
                    playerPlane.keyReleased(e);
                }
            }
        });

        timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!gameOver) {
                    playerPlane.move();
                    moveEnemyPlanes();
                    moveBullets();
                    generateEnemyPlanes();
                    checkCollisions();
                    repaint();
                }
            }
        });
        timer.start();
    }

    private void initGame() {
        playerPlane = new PlayerPlane();
        enemyPlanes = new ArrayList<>();
        bullets = new ArrayList<>();
        random = new Random();
        score = 0;
        gameOver = false;
    }

    private void moveEnemyPlanes() {
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane enemyPlane = enemyPlanes.get(i);
            enemyPlane.move();
            if (enemyPlane.getY() > 600) {
                enemyPlanes.remove(i);
                i--;
            }
        }
    }

    private void moveBullets() {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            bullet.move();
            if (bullet.getY() < 0) {
                bullets.remove(i);
                i--;
            }
        }
    }

    private void generateEnemyPlanes() {
        if (random.nextInt(100) < 10) {
            enemyPlanes.add(new EnemyPlane());
        }
    }

    private void checkCollisions() {
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane enemyPlane = enemyPlanes.get(i);
            for (int j = 0; j < bullets.size(); j++) {
                Bullet bullet = bullets.get(j);
                if (enemyPlane.getBounds().intersects(bullet.getBounds())) {
                    enemyPlanes.remove(i);
                    bullets.remove(j);
                    score++;
                    i--;
                    break;
                }
            }
            if (enemyPlane.getBounds().intersects(playerPlane.getBounds())) {
                gameOver = true;
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.drawString("得分: " + score, 20, 30);
        if (gameOver) {
            g.setFont(new Font("Arial", Font.BOLD, 40));
            g.drawString("游戏结束！按任意键重新开始", 200, 300);
        } else {
            playerPlane.draw(g);
            for (EnemyPlane enemyPlane : enemyPlanes) {
                enemyPlane.draw(g);
            }
            for (Bullet bullet : bullets) {
                bullet.draw(g);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                AdvancedPlaneWarGame game = new AdvancedPlaneWarGame();
                game.setVisible(true);
            }
        });
    }
}

// 玩家飞机类
class PlayerPlane {
    private int x;
    private int y;
    private boolean left;
    private boolean right;
    private boolean up;
    private boolean down;
    private static final int SPEED = 20;
    private List<Bullet> bullets;

    public PlayerPlane() {
        x = 350;
        y = 500;
        this.bullets = new ArrayList<>();
    }

    public void keyPressed(KeyEvent e, List<Bullet> mainBullets) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            left = true;
        }
        if (key == KeyEvent.VK_RIGHT) {
            right = true;
        }
        if (key == KeyEvent.VK_UP) {
            up = true;
        }
        if (key == KeyEvent.VK_DOWN) {
            down = true;
        }
        if (key == KeyEvent.VK_SPACE) {
            Bullet bullet = new Bullet(x + 5, y);
            this.bullets.add(bullet);
            mainBullets.add(bullet);
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            left = false;
        }
        if (key == KeyEvent.VK_RIGHT) {
            right = false;
        }
        if (key == KeyEvent.VK_UP) {
            up = false;
        }
        if (key == KeyEvent.VK_DOWN) {
            down = false;
        }
    }

    public void move() {
        if (left && x > 0) {
            x -= SPEED;
        }
        if (right && x < 750) {
            x += SPEED;
        }
        if (up && y > 0) {
            y -= SPEED;
        }
        if (down && y < 550) {
            y += SPEED;
        }
    }

    public void draw(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(x, y, 20, 20);
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 50, 50);
    }
}

// 敌机类
class EnemyPlane {
    private int x;
    private int y;
    private static final int SPEED = 10;

    public EnemyPlane() {
        Random random = new Random();
        x = random.nextInt(750);
        y = -50;
    }

    public void move() {
        y += SPEED;
    }

    public void draw(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(x, y, 50, 50);
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 50, 50);
    }

    public int getY() {
        return y;
    }
}

// 子弹类
class Bullet {
    private int x;
    private int y;
    private static final int SPEED = 100;

    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void move() {
        y -= SPEED;
    }

    public void draw(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(x, y, 15, 10);
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 30, 30);
    }

    public int getY() {
        return y;
    }
}